package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsSpecificationMapper;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.pojo.DtsGoodsSpecification;
import com.qf.service.DtsGoodsSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DtsGoodsSpecificationServiceImpl implements DtsGoodsSpecificationService {
    @Autowired
    DtsGoodsSpecificationMapper specificationMapper;
    @Override
    public void updateSpecifications(Integer id, DtsGoodsSpecification[] goodsspecifications) {
        LambdaQueryWrapper<DtsGoodsSpecification> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsSpecification::getGoodsId, id);
        specificationMapper.delete(qw);
        if(goodsspecifications != null&&goodsspecifications.length > 0) {
            for (DtsGoodsSpecification specification : goodsspecifications) {
                specification.setAddTime(new Date());
                specification.setGoodsId(id);
                specification.setUpdateTime(new Date());
                specificationMapper.insert(specification);
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        DtsGoodsSpecification goodsSpecification = new DtsGoodsSpecification();
        goodsSpecification.setDeleted(true);
        LambdaQueryWrapper<DtsGoodsSpecification> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsSpecification::getGoodsId, id);
        specificationMapper.update(goodsSpecification, qw);
    }

    @Override
    public void addSpecification(Integer id, DtsGoodsSpecification[] specifications) {
        if(specifications!=null&&specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                specification.setAddTime(new Date());
                specification.setUpdateTime(new Date());
                specification.setGoodsId(id);
                specificationMapper.insert(specification);
            }
        }
    }
}
